package com.mayi.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.alibaba.druid.util.StringUtils;

import java.io.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * @Description: 数据库连接
 * @Auther: yacong_liu
 * @Date: 2019/7/2
 * @version: 1.0
 */
public class DBPoolConnection {

    private static DBPoolConnection instance = null;
    private static DruidDataSource druidDataSource = null;

    static {

        try {
            /*Properties properties = new Properties();
            properties.load(DBPoolConnection.class.getResourceAsStream("druid.properties"));*/

            Properties properties = loadProerties("druid.properties");

            druidDataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 功能描述: 数据库连接池实例 （单例模式）<br>
     *
     * @Return: DBPoolConnection
     * @Author: yacong_liu
     * @Date: 2019/7/3 11:14
     */
    public static synchronized DBPoolConnection getInstance() {
        if (instance == null) {
            instance = new DBPoolConnection();
        }
        return instance;
    }

    /**
     * 功能描述: 从连接池中获取druid数据库连接<br>
     *
     * @Return: DruidPooledConnection druid数据库连接
     * @Author: yacong_liu
     * @Date: 2019/7/3 11:14
     */
    public DruidPooledConnection getConnection() throws SQLException {
        return druidDataSource.getConnection();
    }

    /**
     * 功能描述: 释放或关闭<br>
     *
     * @Param: Connection Statement ResultSet
     * @Author: yacong_liu
     * @Date: 2019/7/3 11:13
     */
    public static void release(Connection connection, Statement statement, ResultSet resultSet) {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            if (statement != null) {
                statement.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            if (connection != null) {
                //归还连接给连接池 并不是真正的关闭连接
                connection.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void release(Connection connection, Statement statement) {
        release(connection, statement, null);
    }

    /**
     * @description: 加载配置文件
     * @param: 文件名
     * @return: Properties
     */
    private static Properties loadProerties(String fileName) {

        if (StringUtils.isEmpty(fileName)) {
            throw new IllegalArgumentException("Properties file path can not be null !");
        }

        String path = DBPoolConnection.class.getClassLoader().getResource("").getPath();
        String rootPath = new File(path).getParent();

        InputStream is = null;
        Properties properties = null;
        try {
            /*
             druid.properties path：web\WEB-INF\druid.properties
             */
            is = new FileInputStream(new File(rootPath + File.separator + fileName));
            properties = new Properties();
            properties.load(is);
        } catch (FileNotFoundException e) {
            System.out.println("数据库配置文件未找到！" + e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return properties;
    }

}
